//
//  MusicListViewController.m
//  MusicPlayer
//
//  Created by lanou3g on 16/5/11.
//  Copyright © 2016年 韩宏. All rights reserved.
//

#import "MusicListViewController.h"
#import "MusicTableViewCell.h"
#import "MusicInfoHandle.h"
#import "AVPlayerManager.h"
#import "MusicViewController.h"
#import "MusicInfo.h"
#import "UIView+Extention.h"

#define kMusicURL @"http://project.lanou3g.com/teacher/UIAPI/MusicInfoList.plist"

@interface MusicListViewController ()

@end

@implementation MusicListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"MusicTableViewCell" bundle:nil] forCellReuseIdentifier:@"cellID"];
    self.title = @"音乐列表";
    [self p_setupDatasource];
    
    UIButton *music = [UIButton buttonWithType:UIButtonTypeCustom];
    music.frame = CGRectMake(0, 0, 40, 40);
    [music setImage:[UIImage imageNamed:@"musicPage"] forState:UIControlStateNormal];
    [music setImage:[UIImage imageNamed:@"musicPage_selected"] forState:UIControlStateHighlighted];
    [music addTarget:self action:@selector(intoMusicPageClicked:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:music];
    
    self.indexP = [NSIndexPath indexPathForRow:0 inSection:0];
}

- (void)intoMusicPageClicked:(UIButton *)sender
{
    [self tableView:self.tableView didSelectRowAtIndexPath:self.indexP];
}

- (void)p_setupDatasource
{
    [[MusicInfoHandle shareHandle] getMusicInfosWithUrl:kMusicURL completion:^(NSArray *musicInfo, NSError *error) {
        if (error) {
            NSLog(@"%@", error);
        } else {
            [self.tableView reloadData];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[MusicInfoHandle shareHandle] numberOfRowsInSection:section];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MusicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID" forIndexPath:indexPath];
    
    MusicInfo *musicInfo = [[MusicInfoHandle shareHandle] musicCellForRowAtIndexPath:indexPath];
    
    [cell setValuesWithModel:musicInfo];
    
    return cell;
}

//实现cell从左向右动画滑出的代理方法
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.x = -self.view.width;
    [UIView transitionWithView:cell duration:0.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
        cell.x = 0;
        cell.alpha = 1;
    } completion:nil];
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.indexP = indexPath;
    MusicViewController *musicVC = nil;
    MusicInfo *musicInfo = [[MusicInfoHandle shareHandle] musicCellForRowAtIndexPath:self.indexP];
    if ([AVPlayerManager shareHandle].playVC && ((MusicViewController *)[AVPlayerManager shareHandle].playVC).musicInfo.musicId == musicInfo.musicId) {
        musicVC = (MusicViewController *)[AVPlayerManager shareHandle].playVC;
    } else {
        musicVC = [[MusicViewController alloc] init];
        musicVC.musicInfo = musicInfo;
        musicVC.index = indexPath.row;
        [AVPlayerManager shareHandle].playVC = musicVC;
    }
    [self presentViewController:musicVC animated:YES completion:nil];
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
